﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Workflow.Project
{
    public partial class SampleIn : System.Web.UI.Page
    {
        public PJSampleIn result = null;
        private PJSampleInManager objPJSampleInManager = new PJSampleInManager();
        private WorkflowManager objWorkflowManager = new WorkflowManager();
        private UserTeamRoleManager objUserTeamRoleManager = new UserTeamRoleManager();
        public int workParentId;
        public int fromPage;
        public int pjCount;
        public int statusNo;
        //public static string serialNum;
        protected void Page_Load(object sender, EventArgs e)
        {
            workParentId = Convert.ToInt32(Request.QueryString["WorkParentId"]);
            fromPage = Convert.ToInt32(Request.QueryString["FromPage"]);
            if (!IsPostBack)
            {
                try
                {
                    pjCount = objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 6, 17) + objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 7, 17);
                    statusNo = objWorkflowManager.GetWorkflowStatues(workParentId);
                    result = objPJSampleInManager.GetSampleIn(workParentId);
                    if (result != null)
                    {
                        if (result.OrderType == "报废")
                        {
                            ddlOrderPurpose.Items[0].Text = "FOT首样";
                            ddlBusinessReason.Items[0].Text = "FA测量";
                        }
                        else
                        {
                            ddlOrderPurpose.Items[0].Text = "转储";
                            ddlBusinessReason.Items[0].Text = "中央仓库";
                        }
                        txtSerialNum.Text = result.SerialNum;
                        ddlOrderType.SelectedItem.Text = result.OrderType;
                        ddlOrderPurpose.SelectedItem.Text = result.OrderPurpose;
                        ddlBusinessReason.SelectedItem.Text = result.BusinessReason;
                        txtMaktx.Text = result.Maktx;
                        txtNum.Text = Convert.ToString(result.Num);
                        txtSapBill.Text = result.SapBill;
                        txtData.Text = result.Statues;
                        if (fromPage == 0 || statusNo != 100)
                        {
                            if (pjCount > 0 && statusNo < 1) //项目
                            {
                                EnabledControl2();
                                Update1.Visible = true;
                            }
                            if (pjCount > 0 && statusNo == 2) //项目仓库
                            {
                                txtSapBill.Enabled = true;
                                Update2.Visible = true;
                            }
                        }
                        if (statusNo == 100)
                        {
                            Update2.Visible = true;
                        }
                    }
                    else
                    {
                        if (statusNo == 0)
                        {
                            EnabledControl1();
                            EnabledControl2();
                            Create.Visible = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                }
            }
            
        }

        protected void Create_Click(object sender, EventArgs e)
        {
            if (ddlOrderType.SelectedValue== "0")
            {
                ddlOrderPurpose.Items[0].Text = "FOT首样";
                ddlBusinessReason.Items[0].Text = "FA测量";
            }
            else
            {
                ddlOrderPurpose.Items[0].Text = "转储";
                ddlBusinessReason.Items[0].Text = "中央仓库";
            }
            try
            {
                PJSampleIn objPJSampleIn = new PJSampleIn()
                {
                    WorkParentId = workParentId,
                    SerialNum = ddlOrderType.SelectedValue + ddlOrderPurpose.SelectedValue + ddlBusinessReason.SelectedValue,
                    OrderType = ddlOrderType.SelectedItem.Text,
                    OrderPurpose = ddlOrderPurpose.SelectedItem.Text,
                    BusinessReason = ddlBusinessReason.SelectedItem.Text,
                    Maktx = txtMaktx.Text.ToString().Trim(),
                    Num = Convert.ToInt32(txtNum.Text.ToString().Trim())
                };
                string createResult = objPJSampleInManager.InsertIsNoSuccess(objPJSampleIn);
                if (createResult != "false")
                {
                    string msg = string.Format("<script>alert('流水号:{0}保存成功！')</script>", createResult);
                    string str = string.Format("<script>window.close();</script>");
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
                }
                else
                {
                    string msg = string.Format("<script>alert('保存失败！')</script>", result);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                }

            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        protected void Update1_Click(object sender, EventArgs e) //项目更改
        {
            PJSampleIn objPJSampleIn = new PJSampleIn()
            {
                WorkParentId = workParentId,
                Maktx = txtMaktx.Text.ToString().Trim(),
                Num = Convert.ToInt32(txtNum.Text.ToString().Trim())
            };
            try
            {
                objPJSampleInManager.UpdateInfo(objPJSampleIn);
                string msg = string.Format("<script>alert('保存成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        protected void Update2_Click(object sender, EventArgs e)
        {
            try
            {
                objPJSampleInManager.UpdateSapBill(txtSapBill.Text.ToString().Trim(), workParentId);
                objPJSampleInManager.UpdateStatues(workParentId);
                string msg = string.Format("<script>alert('保存成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        void EnabledControl1()
        {
            ddlOrderType.Enabled = true;
            ddlOrderPurpose.Enabled = true;
            ddlBusinessReason.Enabled = true;
        }
        void EnabledControl2()
        {
            txtMaktx.Enabled = true;
            txtNum.Enabled = true;
        }

        
    }
}